package com.onpositive.gamemaker.spriteeditor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import com.onpositive.gamemaker.editor.GameCreatorEditorPlugin;
import com.onpositive.gamemaker.swt.SWTImageManager;

public class EditorImageManager 
{

	private final String basePath;
	private static final RGB WHITE_RGB = new RGB(255,255,255);
	private static HashMap<String, Image> imageMap = new HashMap<String, Image>();

	public EditorImageManager(String basePath)
	{
		this.basePath = basePath;
	}
	
	
	public Image getImage(String id)
	{
		Image image = imageMap.get(id);
		if (image == null)
		{
			try
			{
				image = new Image(Display.getDefault(), getResourceStreamById(id));
			} catch (FileNotFoundException e)
			{
				GameCreatorEditorPlugin.logError(e);
			}
			imageMap.put(id, image);
		}
		return image;
	}
	
	public Image getImage(String id, boolean transparent)
	{
		if (!transparent)
			return getImage(id);
		else
		{
			Image image = imageMap.get(id);
			if (image == null)
			{
				try
				{
					ImageData data = getResourceStreamById(id);
					int whitePixel = data.palette.getPixel(WHITE_RGB);
					data.transparentPixel = whitePixel;
					image = new Image(Display.getDefault(),data);
					imageMap.put(id, image);
				} catch (FileNotFoundException e)
				{
					GameCreatorEditorPlugin.logError(e);
				}
			}
			return image;
		}
	}

	protected ImageData getResourceStreamById(String id) throws FileNotFoundException
	{
		int idx = id.indexOf('[');
		if (idx < 0)
		{
			String path = basePath + File.separator + id;
			File file = new File(path);
			return new ImageData(new FileInputStream(file));
		}
		int x,y,width,height;
		int startIdx = idx;
		int endIdx = id.lastIndexOf(']');
		if (endIdx < startIdx)
			endIdx = id.length();
		String bounds = id.substring(startIdx + 1, endIdx);
		id = id.substring(0,startIdx).trim();
		StringTokenizer tokenizer = new StringTokenizer(bounds,",");
		x = Integer.parseInt(tokenizer.nextToken());
		y = Integer.parseInt(tokenizer.nextToken());
		width = Integer.parseInt(tokenizer.nextToken());
		height = Integer.parseInt(tokenizer.nextToken());
		ImageData baseData = getResourceStreamById(id);
		Image baseImg = new Image(Display.getDefault(), baseData);
		Image resultImg = new Image(Display.getDefault(),new Rectangle(0,0,width, height));
		GC gc = new GC(resultImg);
		gc.drawImage(baseImg,x,y,width,height,0,0,width,height);
		return resultImg.getImageData();
	}

	/**
	 * Gets full img descriptor, ignoring given region
	 * @param id
	 * @return 
	 */
	public Image getFullImage(String id)
	{
		int idx = id.indexOf('[');
		if (idx < 0)
			return getImage(id);
		return getImage(id.substring(0,idx).trim());
		
	}
	
	

}
